<?php

class player extends model{
	private $_Name;
	private $_Surname;
	private $_Birthdate;
	private $_CountryId;
	private $_Points;
	private $_Ranking;

	private function _setDefaultValues($arrInitialValues = null){
		if (isset($arrInitialValues['name']) && !empty($arrInitialValues['name']))
			$this->setName($arrInitialValues['name']);
		else 
			$this->setName('');
			
		if (isset($arrInitialValues['surname']) && !empty($arrInitialValues['surname']))
			$this->setSurname($arrInitialValues['surname']);
		else 
			$this->setSurname('');
			
		if (isset($arrInitialValues['birthdate']) && !empty($arrInitialValues['birthdate']))
			$this->setBirthdate($arrInitialValues['birthdate']);
		else 
			$this->setBirthdate(date('Y-m-d'));
			
		if (isset($arrInitialValues['country_id']) && !empty($arrInitialValues['country_id']))
			$this->setCountryId($arrInitialValues['country_id']);
		else 
			$this->setCountryId(0);

		if (isset($arrInitialValues['points']) && !empty($arrInitialValues['points']))
			$this->setPoints($arrInitialValues['points']);
		else 
			$this->setPoints(0);
			
		if (isset($arrInitialValues['ranking']) && !empty($arrInitialValues['ranking']))
			$this->setRanking($arrInitialValues['ranking']);
		else 
			$this->setRanking(0);
	}
	
	
	public function __construct($arrInitialValues = null){
		$this->setTable('players');
		$this->setColumnId('player_id');

		$this->_setDefaultValues($arrInitialValues);
		parent::__construct($arrInitialValues);
		
		$Id = $this->getId();
		if (!empty($Id))
			$this->_getMe();
	}
	
	public function GetTopTen($country_id, $sex){
		$arrPlayers = array();
		
		$topTen = $this->_GetTopTen($country_id, $sex);
		foreach ($topTen as $player){
			$arrPlayers[] = new player(
				array(
					"id" => $player["player_id"],
					"name" => $player["name"],
					"surname" => $player["surname"],
					"ranking" => $player["ranking"],
					"points" => $player["points"],
					"country" => $player["country_id"],
					"birthdate" => $player["birthdate"]
				)
			);
		}
		return $arrPlayers;
	}
	
	private function _GetTopTen($country_id, $sex){
		$query = '
			SELECT * 
			FROM players 
			WHERE sex = "' . $sex . '" 
				AND active = 1 ';
		if ($country_id)
			$query .= '
			AND country_id = ' . $country_id;
		$query .= '
			ORDER BY points DESC
			LIMIT 0, 10';

		$dbHelper = new DBHelper($query);
		return $dbHelper->getResult();	
	}
	
	private function _getMe(){
		$me = $this->getOne();
		$this->_setDefaultValues($me[0]);
	}
	
	/**
	 * @return the $_Name
	 */
	public function getName() {
		return $this->_Name;
	}

	/**
	 * @return the $_Surname
	 */
	public function getSurname() {
		return $this->_Surname;
	}

	/**
	 * @return the $_Birthdate
	 */
	public function getBirthdate() {
		return $this->_Birthdate;
	}

	/**
	 * @return the $_CountryId
	 */
	public function getCountryId() {
		return $this->_CountryId;
	}

	/**
	 * @return the $_Points
	 */
	public function getPoints() {
		return $this->_Points;
	}

	/**
	 * @return the $_Ranking
	 */
	public function getRanking() {
		return $this->_Ranking;
	}

	/**
	 * @param field_type $_Name
	 */
	public function setName($_Name) {
		$this->_Name = $_Name;
	}

	/**
	 * @param field_type $_Surname
	 */
	public function setSurname($_Surname) {
		$this->_Surname = $_Surname;
	}

	/**
	 * @param field_type $_Birthdate
	 */
	public function setBirthdate($_Birthdate) {
		$this->_Birthdate = $_Birthdate;
	}

	/**
	 * @param field_type $_CountryId
	 */
	public function setCountryId($_CountryId) {
		$this->_CountryId = $_CountryId;
	}

	/**
	 * @param field_type $_Points
	 */
	public function setPoints($_Points) {
		$this->_Points = $_Points;
	}

	/**
	 * @param field_type $_Ranking
	 */
	public function setRanking($_Ranking) {
		$this->_Ranking = $_Ranking;
	}
}

?>